#include "dstack.h"

typedef struct node *Nodeptr;

struct node{
	element_t data;
	Nodeptr previous;
	Nodeptr next;
	};

struct stack_data{
	Nodeptr front;
	Nodeptr back;
	unsigned int size;
	};

stack create(void)
{stack ptr;
 ptr = malloc(sizeof(struct stack_data));
 if(ptr == NULL)
	return NULL;
 ptr->size=0;
 ptr->back = NULL;
 ptr->front = NULL;
 return ptr;
}

void clear(stack target_stack)
{Nodeptr temp = target_stack->back,temp1;
 while(temp != NULL){
	temp1 = temp;
	temp = temp->next;
	free(temp1);
	}
 target_stack->size = 0;
 target_stack->front = NULL;
 target_stack->back = NULL;
}

void delete(stack target_stack)
{Nodeptr temp = target_stack->back,temp1;
 while(temp != NULL){
	temp1 = temp;
	temp = temp->next;
	free(temp1);
	}
 free(target_stack);
}



unsigned int size(stack target_stack)
{return target_stack->size;}

bool empty(stack target_stack)
{if(size (target_stack) == 0)
	return true;
 return false;
}


bool push_back(stack target_stack, element_t data)
{Nodeptr p;
 p=malloc(sizeof(struct node));
 if(p == NULL)
	return false;
 p->data = data;
 if(size(target_stack) == 0){
	target_stack->front = p;
	target_stack->back = p;
	p->next=NULL;
	p->previous=NULL;
	}
 else{
	target_stack->back->previous = p;
	p->next = target_stack->back;
	p->previous = NULL;
	target_stack->back = p;	
	}
 target_stack->size++;
 return true;
}

bool push_front(stack target_stack, element_t data)
{Nodeptr p;
 p = malloc(sizeof(struct node));
 if(p == NULL)
	return false;
 p->data = data;
 if(size(target_stack) == 0){
	target_stack->front = p;
	target_stack->back = p;
	p->next=NULL;
	p->previous=NULL;
	}
 else{
	target_stack->front->next = p;
	p->previous = target_stack->front;
	p->next= NULL;
	target_stack->front = p;
	}
 target_stack->size++;
 return true;
}


bool pop_back(stack target_stack)
{Nodeptr temp;
 if(size(target_stack) == 0)
	return false;
 temp = target_stack->back;
 target_stack->back = target_stack->back->next;
 free(temp);
 target_stack->back->previous = NULL;
return true;
}




bool pop_front(stack target_stack)
{Nodeptr temp;
 if(size(target_stack) == 0)
	return false;
 temp = target_stack->front;
 target_stack->front = target_stack->front->previous;
 free(temp);
 target_stack->front->next = NULL;
 return true;
}



bool front(stack target_stack, element_t *data)
{if(size(target_stack) == 0)
	return false;
 *data = target_stack->front->data;
 return true;
 }

bool back(stack target_stack, element_t *data)
{if(size(target_stack) == 0)
	return false;
 *data = target_stack->back->data;
 return true;
}

























